package org.example.config;
import lombok.extern.slf4j.Slf4j;
import org.example.handler.MyAccessDeniedHandler;
import org.example.handler.MyAuthenticationFailureHandler;
import org.example.handler.MyAuthenticationSuccessHandle;
import org.example.handler.MyLogoutSuccessHandler;
import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import javax.annotation.Resource;
/**
 * @Author：无敌暴龙战士
 * @Package：org.example.config
 * @Project：learn_spring_security
 * @name：WebSecurityConfig
 * @Date：2025/2/12 3:06
 * @Filename：WebSecurityConfig
 * @Description:
 */

@Slf4j
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    MyAuthenticationSuccessHandle myAuthenticationSuccessHandle;
    @Resource
    MyAuthenticationFailureHandler myAuthenticationFailureHandler;
    @Resource
    MyAccessDeniedHandler myAccessDeniedHandler;
    @Resource
    MyLogoutSuccessHandler myLogoutSuccessHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //任何访问均需要认证
        http.authorizeRequests().anyRequest().authenticated();
        //登录放行
        http.formLogin()
                .successHandler(myAuthenticationSuccessHandle)         //配置认证成功处理器
                .failureHandler(myAuthenticationFailureHandler)        //配置认证失败处理器
                .permitAll();
        http.logout().logoutSuccessHandler(myLogoutSuccessHandler);
        http.exceptionHandling().accessDeniedHandler(myAccessDeniedHandler);
    }
}
